
import SDKBase from "./SDKBase";
import { SDKState, ADName, ResultCallback, ShareInfo, ShareType } from "../SDKDefine";
/**
 * 分享
 */
export default class SDKShare extends SDKBase {

    protected callback: (r: SDKState) => void;

    protected _cfg: ShareType = null;

    // protected data: ShareInfo[];

    init(data: ShareType) {
        this._cfg = data;
        this.sdk.showShareMenu({
            withShareTicket: true,
            menus: ['shareAppMessage', 'shareTimeline']
        });

        this.sdk.updateShareMenu({
            withShareTicket: true
        })



        this.sdk.onShareAppMessage(() => {
            // 用户点击了“转发”按钮
            return this.getData(0)
        })
    }

    has() {
        return this.sdk && this._cfg
    }

    open(param: { index: number, func?: ResultCallback, path?: string }) {
        this.callback = param.func;
        this.sdk.shareAppMessage(this.getData(param.index, param.path))
    }

    protected getData(index: number, path?: string): any {
        let cfg: ShareInfo = this._cfg.shares[index]
        let data = {
            title: cfg[ADName.shareTitle],
            imageUrl: cfg[ADName.shareImage],
            imageUrlId: cfg[ADName.imageUrlId],
            success: () => {
                if (this.callback) {
                    this.callback(SDKState.SUCCESS)
                }
            },
            fail: (e: any) => {
                console.log("分享失败");
                if (this.callback) {
                    this.callback(SDKState.FAIL)
                }
            },
        }
        return data;
    }





    getShareInfo(shareTicket: string, func: (result: SDKState) => void) {
        if (shareTicket) {
            this.sdk.getShareInfo({
                shareTicket: shareTicket,
                success: (res: any) => {
                    func(res)
                },
                fail: () => {

                }
            });
        }

    }

}
